/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package easyrgb.frame;

import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import easyrgb.constance.CConfig;
import easyrgb.constance.Language;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author tandn
 */
public class CMainWindow extends JFrame
{
    //window instance
    private static CMainWindow m_window;
    //menu
    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenu editMenu;
    private JMenu helpMenu;
    private JMenu aboutMenu;
    private JMenuItem fileNew;
    private JMenuItem fileOpen;
    private JMenuItem fileSave;
    private JMenuItem fileSaveAs;
    //panel
    private CCenterPanel m_centerPanel;

    private CMainWindow()
    {
        setTitle(Language.WINDOW_TITLE[CConfig.LANGUAGE]);
        setSize(CConfig.WINDOW_WIDTH, CConfig.WINDOW_HEIGHT);
        //menu
        initMenuBar();
        //add panel
        m_centerPanel = new CCenterPanel();
        add(m_centerPanel);
        //
        if (CConfig.IS_DEBUG)
        {
            getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Exit");
            getRootPane().getActionMap().put("Exit", new AbstractAction()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    System.exit(0);
                }
            });
        }
        try
        {
            //UIManager.setLookAndFeel(new MetalLookAndFeel());
            //UIManager.setLookAndFeel(new MotifLookAndFeel());
            //UIManager.setLookAndFeel(new MultiLookAndFeel());
            //UIManager.setLookAndFeel(new NimbusLookAndFeel());
            //UIManager.setLookAndFeel(new SynthLookAndFeel());
            //UIManager.setLookAndFeel(new WindowsClassicLookAndFeel());
            UIManager.setLookAndFeel(new WindowsLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex)
        {
            Logger.getLogger(CMainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public synchronized static CMainWindow getInstance()
    {
        if (m_window == null)
        {
            m_window = new CMainWindow();
        }
        return m_window;
    }

    private void initMenuBar()
    {
        //init menubar
        menuBar = new JMenuBar();
        //add menu file
        fileMenu = new JMenu(Language.MENU_FILE[CConfig.LANGUAGE]);
        fileMenu.setMnemonic(Language.MENU_FILE[CConfig.LANGUAGE].charAt(0));
        fileNew = new JMenuItem(Language.MENU_FILE_NEW[CConfig.LANGUAGE], null);
        fileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_MASK));
        fileMenu.add(fileNew);
        fileOpen = new JMenuItem(Language.MENU_FILE_OPEN[CConfig.LANGUAGE], null);
        fileMenu.add(fileOpen);
        fileSave = new JMenuItem(Language.MENU_FILE_SAVE[CConfig.LANGUAGE], null);
        fileMenu.add(fileSave);
        fileSaveAs = new JMenuItem(Language.MENU_FILE_SAVE_AS[CConfig.LANGUAGE], null);
        fileMenu.add(fileSaveAs);

        menuBar.add(fileMenu);
        //add menu edit
        editMenu = new JMenu(Language.MENU_EDIT[CConfig.LANGUAGE]);
        editMenu.setMnemonic(Language.MENU_EDIT[CConfig.LANGUAGE].charAt(0));
        menuBar.add(editMenu);
        //add menu help
        helpMenu = new JMenu(Language.MENU_HELP[CConfig.LANGUAGE]);
        helpMenu.setMnemonic(Language.MENU_HELP[CConfig.LANGUAGE].charAt(0));
        menuBar.add(helpMenu);
        //add menu about
        aboutMenu = new JMenu(Language.MENU_ABOUT[CConfig.LANGUAGE]);
        aboutMenu.setMnemonic(Language.MENU_ABOUT[CConfig.LANGUAGE].charAt(0));
        menuBar.add(aboutMenu);

        //add to frame
        this.setJMenuBar(menuBar);
    }

    public static void debug(String s)
    {
        if (CConfig.IS_DEBUG)
        {
            System.out.println("" + s);
        }
    }
}

